<?php
/***************************************************************************
 *
 *   copyright            : (C) 2011 Winds of Storm
 *
 *   $Id: timers.php 131 2011-06-12 20:24:56Z stormerider $
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free softtimere; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Softtimere Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

$timer_commands	= array('TYPE'		=>	'command',
			'NAME'		=>	'Timer Commands',
			'VERSION'	=>	'0.0.1',
			'CREDIT'	=>	'StormeRider',
			'FUNC'		=>	'pluginTimerCommands',
			'HELP'		=>	'pluginTimerCommandsHelp');
$plugins[]	= $timer_commands;

function pluginTimerCommandsHelp($help) {
	global	$timer_commands;

	$help['timer1']		= array("/timer [number] [message]",
					"Set a [number] minute timer. [message] will be sent when the timer has been completed." .
					pluginCredit($timer_commands));
	$help['canceltimers']	= array("/canceltimers",
					"Cancel all scheduled alarm timers. (Moderator only)" . 
					pluginCredit($timer_commands));

	return $help;
}

function pluginTimerCommands($userinfo, $event) {
	global	$db;
	global	$dbprefix;
	global	$p_row;

	$userid		= $userinfo['userid'];
	$username	= $userinfo['username'];
        $isMod          = $userinfo['isMod'];

	$msg	= $event[0]['msg'];
	$room	= $event[0]['room'];

	if (eregi("^/timer", $msg)) {
		$event[0]['parsed']	= 1;
		$timertime		= explode(" ", $msg, 3);
		if (!is_numeric($timertime[1]) || $timertime[2] == "") {
			$event[0]['room']	= 0;
			$event[0]['userto']	= $userid;
			$event[0]['msg']	= addslashes("<span class=sysmsg>*** Error: syntax is: /timer [minutes] [message].</span>");
		} else {
			$time			= $timertime[1];
			$event[0]['msg']	= addslashes("<span class=sysmsg><i>*** $username starts a $time minute timer!</i></span>");
			$sql	= "INSERT INTO {$dbprefix}alarms (alarm_user_id, alarm_room_to, alarm_time, alarm_text)
					VALUES ($userid, $room, NOW()+INTERVAL '$time' MINUTE, '<span class=sysmsg><i>*** Timer started by " . addslashes($username) . " has ended: " . addslashes($timertime[2]) . "</i></span>')";
			$db->sql_query($sql);
		}
	} else if (eregi("^/canceltimers", $msg)) {
		$event[0]['parsed']	= 1;
		$event[0]['room']	= 0;
		$event[0]['userto']	= $userid;
                if (!$isMod) {
                        $event[0]['msg']        = addslashes('<span class=sysmsg><b>*** ERROR: you are not authorized to cancel timers.</b></span>');
		} else {
                        $event[0]['msg']        = addslashes('<span class=sysmsg><b>*** All future timers cancelled.</b></span>');

			$sql	= "UPDATE {$dbprefix}alarms SET alarm_inactive=1 WHERE alarm_inactive=0";
			$db->sql_query($sql);
		}
	}
}
?>
